<?php

namespace App\Http\Controllers\Api;

use App\Models\OpLogsModel;
use App\Models\OpLogsSearchModel;
use App\Services\ErrorCodeService;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;

class ApiBaseController extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    //全局参数
    protected $_param = [];

    /**
     * Controller constructor.
     */
    public function __construct(Request $request)
    {

    }

    /**
     * 统一成功返回
     * @param array $data
     * @param int $code
     * @param string $message
     * @return object|null
     */
    public static function codeSuccess(array $data = [], int $code = 200, string $message = '操作成功'): ?object
    {
        return response()->json([
            'code' => $code,
            'message' => $message,
            'data' => $data
        ]);
    }

    /**
     * 统一成功返回
     * @param array $data
     * @param int $code
     * @param string $message
     * @return object|null
     */
    public static function codeError(array $data = [], int $code = 1001, string $message = '操作失败'): ?object
    {
        return response()->json([
            'code' => $code,
            'message' => $message,
            'data' => $data
        ]);
    }

    /**
     * @param $params
     * @param $field
     * @return bool
     */
    public function handleIsNull($params, $field): bool
    {
        foreach ($field as $value){
            if (empty($params[$value])){
                return false;
            }

        }

        return true;
    }

    public function createOp($style, $taskId, $tag = '', $content = '', $type = 0)
    {
        //1vip任务创建
        $data['created_at'] = time();
        $data['task_id'] = $taskId;
        $data['op_user_id'] = adminInfo('id');
        $data['style'] = $style;

        switch ($style){
            case 1:
                $data['msg'] = 'vip任务创建';
                break;

            case 2:
                $data['msg'] = '任务取消';
                break;

            case 3:
                $data['msg'] = '任务删除';
                break;

            case 4:
                $data['msg'] = '任务驳回';
                break;

            case 5:
                $data['msg'] = '任务通过';
                break;
            case 6:
                $data['msg'] = '任务移交';
                break;
            case 999:
                $data['msg'] = '任务转移';
                break;
            case 998:
                $data['msg'] = '修改任务';
                break;

            case 997:
                $data['msg'] = '任务加标签：' . $tag;
                break;

            case 996:
                $data['msg'] = '重新发布';
                break;

            case 995:
                $data['msg'] = '超时处理';
                break;

            case 994:
                $data['msg'] = '超时移交';
                break;

            case 993:
                $data['msg'] = '用户维护';
                break;

            case 992:
                $data['msg'] = '超时回访';
                break;

            case 991:
                $data['msg'] = '催单处理';
                break;

            case 990:
                $data['msg'] = '禁用申请';
                break;

            case 989:
                $data['msg'] = '申请取消';
                break;

            case 988:
                $data['msg'] = '申请通过';
                break;

            case 987:
                $data['msg'] = '申请驳回';
                break;

            case 986:
                $data['msg'] = '禁用用户';
                break;

            case 985:
                $data['msg'] = '启用用户';
                break;

            case 984:
                $data['msg'] = '激活移交';
                break;

            case 983:
                $data['msg'] = '激活处理';
                break;
            case 982:
                $data['msg'] = '回访申请';
                break;
            case 41:
                $data['msg'] = '引流';
                break;

            case 42:
                $data['msg'] = '匹配';
                if (!empty($content)){
                    $data['content'] = json_encode($content);
                }
                break;

            case 43:
                $data['msg'] = '搜索';
                break;

            case 44:
                $data['msg'] = '完成';
                break;
        }

        if (!empty($type)){
            return OpLogsSearchModel::insertSingle($data);
        }else{
            //添加
            return OpLogsModel::insertSingle($data);
        }
    }

}
